package com.sc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sc.controller.base.BaseController;
import com.sc.dao.OrderDao;
import com.sc.dao.ShopCartDao;
import com.sc.model.JResult;
import com.sc.model.Order;
import com.sc.model.ShopCart;
import com.sc.model.User;
import com.sc.service.OrderService;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

	@Resource
	private OrderService orderService;

	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private ShopCartDao shopCartDao;

	@RequestMapping("/my")
	public String myOrder(Model model,HttpServletRequest req) {
		User user =(User)req.getSession().getAttribute("user");
		model.addAttribute("user", user);
		return "order";
	}
	
	@RequestMapping("/settlement")
	public String settlement(String shopCartIds, HttpServletRequest req) {
		shopCartIds = "1c9a01868c144d91bab190a5e5a90f83,45tf24bb0c4b4fb6b64c724c31731543,68f79c4376ad429ea6abac1b7f0b6170";
		req.setAttribute("shopCartIds", shopCartIds);
		return "settlement";
	}
	
	@RequestMapping("/settlement-pay")
	public String settlementPay(String shopCartIds, HttpServletRequest req) {
		List<ShopCart> shopCarts = shopCartDao.getByIds(shopCartIds.split(","));
		req.setAttribute("shopCarts", shopCarts);
		return "settlement#pay";
	}
	
	@RequestMapping("/settlement-order")
	public JResult settlementOrder(String shopCartIds, HttpServletRequest req) {
		Order order = new Order(getUserId());
		orderDao.insertTemplate(order);
		List<ShopCart> shopCarts = shopCartDao.getByIds(shopCartIds.split(","));
		for (ShopCart shopCart : shopCarts) {
			shopCart.setIsDelete(1);
			shopCart.setOrderId(order.getId());
		}
		return JResult.SUCCESS;
	}
	
	@RequestMapping("/search")
	public String searchPage(HttpServletRequest req) {
		Object page = req.getParameter("page");
		page = page == null ? getSessionAttribute("page") == null ? "1" : getSessionAttribute("page") : page;
		setSessionAttribute("page", page);
		updateTable(Integer.parseInt(page.toString()), req);
		return "order#test";
	}

	@RequestMapping("/confirm-receipt")
	public String confirmReceipt(HttpServletRequest req) {
		orderDao.updateTemplateById(new Order(req.getParameter("id"), Integer.parseInt(req.getParameter("status"))));
		updateTable(Integer.parseInt(req.getParameter("page")), req);
		return "order#test";
	}

	@RequestMapping("/delete")
	public String delete(HttpServletRequest req) {
		orderDao.deleteById(req.getParameter("id"));
		updateTable(Integer.parseInt(req.getParameter("page")), req);
		return "order#test";
	}

	private void updateTable(int page, HttpServletRequest req) {
		req.setAttribute("orderVos", orderService.getOrdersByUserId(getUserId(), page));
	}

}
